package com.tencent.sr.iris.activity.dependency.qufa;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.tencent.sr.iris.activity.common.config.QuFaConfig;
import com.tencent.sr.iris.activity.common.util.QuFaHttpUtil;
import com.tencent.sr.iris.activity.interfaces.request.group.AdvisorGroupTaskSecondRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.AdvisorGroupTaskThirdRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.CanJoinTaskRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.GroupTaskSecondRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.TaskStatisticsRequest;
import com.tencent.sr.iris.activity.interfaces.response.group.AdvisorNormalTaskSecondResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.GroupNormalTaskSecondResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.TaskStatisticsResponse;
import com.tencent.sr.iris.activity.dependency.dto.common.GroupTaskBoardUtils;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardNormalSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.CanJoinTaskDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.GroupNormalTaskSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.GroupTaskBaseDTO;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import com.tencent.sr.rmall.common.response.PaginationResponse;
import com.tencent.sr.rmall.springbootstarter.utils.JsonUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class GroupTaskBoardNewDependency {

    @Resource
    private QuFaHttpUtil quFaHttpUtil;
    @Resource
    private QuFaConfig config;

    /**
     * 团长视角-团推团（普通/特邀）二级页面
     *
     * @param request
     * @param url
     * @return
     */
    public PaginationResponse<GroupNormalTaskSecondResponse> queryNormalTaskBoard(
            PaginationRequest<GroupTaskSecondRequest> request, String url) {
        PaginationResponse<GroupNormalTaskSecondResponse> response = new PaginationResponse<>();
        String reqBody = GroupTaskBoardUtils.toGroupTaskSecondBoardBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<GroupNormalTaskSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<GroupNormalTaskSecondDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<GroupNormalTaskSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<GroupNormalTaskSecondResponse> list = data1.stream()
                        .map(dto -> GroupTaskBoardUtils.toGroupNormalTaskSecondResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 导师视角-团推团（普通/特邀）二级页面
     *
     * @param request
     * @param url
     * @return
     */
    public PaginationResponse<AdvisorNormalTaskSecondResponse> queryAdvisorNormalTaskBoard(
            PaginationRequest<AdvisorGroupTaskSecondRequest> request, String url) {
        PaginationResponse<AdvisorNormalTaskSecondResponse> response = new PaginationResponse<>();
        String reqBody = GroupTaskBoardUtils.toAdvisorSecondBoardInviteBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<AdvisorBoardNormalSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<AdvisorBoardNormalSecondDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<AdvisorBoardNormalSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<AdvisorNormalTaskSecondResponse> list = data1.stream()
                        .map(dto -> GroupTaskBoardUtils.toAdvisorNormalTaskSecondResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 导师视角-团推团（普通/特邀）三级页面
     *
     * @param request
     * @param url
     * @return
     */
    public PaginationResponse<GroupNormalTaskSecondResponse> queryAdvisorNormalThirdBoard(
            PaginationRequest<AdvisorGroupTaskThirdRequest> request, String url) {
        PaginationResponse<GroupNormalTaskSecondResponse> response = new PaginationResponse<>();
        String reqBody = GroupTaskBoardUtils.toAdvisorThirdBoardBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<GroupNormalTaskSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<GroupNormalTaskSecondDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<GroupNormalTaskSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<GroupNormalTaskSecondResponse> list = data1.stream()
                        .map(dto -> GroupTaskBoardUtils.toGroupNormalTaskSecondResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    public Map<Integer, Integer> queryCanJoinTaskCount(CanJoinTaskRequest request, String url) {
        Map<Integer, Integer> map = new HashMap<>();
        List<CanJoinTaskDTO> canJoinTaskDTOS = quFaHttpUtil.postList(url, request, CanJoinTaskDTO.class);
        if (CollectionUtils.isNotEmpty(canJoinTaskDTOS)) {
            map = canJoinTaskDTOS.stream().collect(Collectors.toMap(CanJoinTaskDTO::getTaskPhaseId,
                    CanJoinTaskDTO::getTotal));
        }
        return map;
    }

    /**
     * 团长任务统计(导师/营销视角)
     *
     * @param request
     * @param url
     * @return
     */
    public TaskStatisticsResponse queryTaskStatistics(TaskStatisticsRequest request, String url) {
        TaskStatisticsResponse taskStatisticsResponse = quFaHttpUtil.postObject(url, request,
                TaskStatisticsResponse.class);
        return taskStatisticsResponse;
    }
}
